-----------------------------------------------------------------------------
-- 羁绊奖励界面
--
---------------
-- Author: wd
-----------------------------------------------------------------------------
require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIFetterAward = {}
HelperFunc.SetUIEnv(UIFetterAward)
local this = UIFetterAward

this.fetterAwardCfg = nil  --羁绊奖励配置表
this.fetterAwardList = nil
this.hadAwardTable = {} --已领取的奖励id
this.hasInit = false
this.btnBack = nil
this.g_awardValue = nil --当前的羁绊点
this.dynamicList = nil
this.txtAmount = nil
this.heroName = nil
this.heroID = nil
function UIFetterAward.Init(heroId)
    if fetterAwardCfg == nil then
        fetterAwardCfg = G.dataTable["t_trammelpointreward"]

        fetterAwardList = {}
        for _, info in pairs(fetterAwardCfg) do
            table.insert(fetterAwardList, info)
        end
        table.sort(fetterAwardList, function (a,b) return a["f_Id"] < b["f_Id"] end)
        --fetterAwardList = table.reverse(fetterAwardList)
    end
    heroID = heroId
    g_awardValue = logicMgr.HeroManager.GetHeroFetterAwardValue()
    --printError("g_awardValue:", g_awardValue)

    local name, packageName = getUIName(PanelResNames.UIFetterAward)
    panelMgr:ShowUI(name, packageName, this.OnCreate, Config.useLocalUI)
end

function UIFetterAward.OnCreate(obj)
    if not hasInit then
        hasInit = true
        btnBack = obj:GetChild("btnClose")
        txtAmount = obj:GetChild("amount")
        --heroName = obj:GetChild("txtHeroName")
        this.titleTips = obj:GetChild("txttips")
        this.titleTips.text = getLanguage("TrammelPointReward_Title");
        local root = obj:GetChild("root")
        dynamicList = root:GetChild("elementGroup")
        dynamicList.itemRenderer = this.refresHeroPosData
        dynamicList:SetVirtual()
        this.addEvent()
    end

    this.UpdateElement()
end

function UIFetterAward.addEvent()
    btnBack.onClick:Add(this.onClickBack)
end

function UIFetterAward.removeEvent()
    btnBack.onClick:Clear()
end

function UIFetterAward.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_FetterAward, this.UpdateElement)
end

function UIFetterAward.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_FetterAward, this.UpdateElement)
end

function UIFetterAward.OnDestroy()
    hasInit = false
    this.removeEvent();
end

function UIFetterAward.OnEnable()
    this.LocalListenEvent();
end

function UIFetterAward.OnDisable()
    this.LocalCloseEvent();
end

--- 初始化已领取的羁绊id
function UIFetterAward.InitHadWaradTable(idList)
    --g_awardValue = logicMgr.HeroManager.GetHeroFetterAwardValue()
    --print("InitHadWaradTable:", g_awardValue)

    if idList ~= nil then
        for _, key in pairs(idList) do
            hadAwardTable[key] = key
        end
    end
end

--- 更新总数值
function UIFetterAward.UpdateAmountValue()
    txtAmount.text = g_awardValue
end

--- 刷新元素内容
function UIFetterAward.UpdateElement()
    hadAwardTable = logicMgr.HeroManager.GetHeroFetterHadAwardList()
    --heroName.text = getLanguage(G.dataTable["t_hero"][tostring(heroID)]["f_HeroName"])
    this.UpdateAmountValue()

    dynamicList.numItems = #fetterAwardList

    local count = table.nums(hadAwardTable)
    --dynamicList:ScrollToView(0)
    if count > dynamicList.numItems then
        count = dynamicList.numItems;
    end
    dynamicList:ScrollToView(count);
end

--- 刷新单个元素信息
function UIFetterAward.refresHeroPosData(index, obj)
    index = index + 1
    local cfgInfo = fetterAwardList[index]

    -- 领取条件
    local txtLimit = obj:GetChild("limit")
    local txtLimit2 = obj:GetChild("limit2")
    txtLimit.text = cfgInfo["f_Point"]
    txtLimit2.text = cfgInfo["f_Point"]

    -- 事件响应
    local getBtn = obj:GetChild("btnGet")
    getBtn.onClick:Clear()
    getBtn.onClick:Add(function()
        this.onClickAward(cfgInfo["f_Id"])
    end)

    -- 按钮状态
    local ctrlState = obj:GetController("c1")
    local ctrlLimit = obj:GetController("c2")
    local position = obj:GetController("position")
    position.selectedIndex = index % 2;
    ctrlState.selectedIndex = 0
    ctrlLimit.selectedIndex = 0
    getBtn.title = getLanguage("button_02");
    if hadAwardTable[cfgInfo["f_Id"]] then
        ctrlState.selectedIndex = 2
        ctrlLimit.selectedIndex = 1
    else
        if g_awardValue >= cfgInfo["f_Point"] then
            ctrlState.selectedIndex = 1
            ctrlLimit.selectedIndex = 1
            getBtn.title = getLanguage("button_01");
        end
    end

    -- 道具奖励
    --local itemList = decodeJsonStrToTabel(cfgInfo["f_Reward"])
    --local function UpdateOneItem(_index, itemObj)
    --    local key = _index + 1
    --    local itemInfo = itemList[key]
    --    local itemID = itemInfo[1]
    --    local itemNum = itemInfo[2]
    --    local itemCfg = G.dataTable["t_item"][tostring(itemID)]
    --
    --    local icon = itemObj:GetChild("ItemIcon")
    --    local txtNum = itemObj:GetChild("n9")
    --    local quality = itemObj:GetChild("quality")
    --    if itemCfg then
    --        icon.url = EnumConst.AtlasPath.ItemAtlas..itemCfg["f_Icon"]
    --        txtNum.text = itemNum
    --        quality.url = EnumConst.AtlasPath.ICONAtlas..itemCfg["f_Quality"]
    --    else
    --        printError("t_trammelpointreward中配置了一个道具表不存在的id"..itemID)
    --    end
    --end
    --local itemGroup = obj:GetChild("itemGroup")
    logicMgr.CostManager.RefreshItemList(obj:GetChild("itemGroup"), decodeJsonStrToTabel(cfgInfo["f_Reward"]), 2);
    --itemGroup.itemRenderer = UpdateOneItem
    --itemGroup.numItems = table.nums(itemList)
end

--- 点击 领取 羁绊奖励
function UIFetterAward.onClickAward(fetterID)
    partnerHandler.sendFetterAward(fetterID)
end

function UIFetterAward.onClickBack()
    closeUI(PanelNames.UIFetterAward)
end

return UIFetterAward
